package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.qo.EmployeeQueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    //文件导入操作
    @RequestMapping("/imported")
    @RequiresPermissions(value={"employee:export","员工导出"},logical= Logical.OR)
    public String imported(MultipartFile file) throws IOException {
        Workbook workbook = new HSSFWorkbook(file.getInputStream());
        employeeService.imported(workbook);
        return "redirect:/employee/list.do";
    }

    //导出操作
    @RequestMapping("/export")
    @ResponseBody
    @RequiresPermissions(value={"employee:export","员工导出"},logical= Logical.OR)
    public void export(HttpServletResponse response) throws IOException {
        response.setHeader("Content-disposition","attachment;filename=employee.xls");
        Workbook workbook = employeeService.export();
        //将这个文件以流的形式写到页面上
        workbook.write(response.getOutputStream());

    }



    // 批量删除员工数据
    @RequestMapping("/batchDelete")
    @ResponseBody
    @RequiresPermissions(value={"employee:batchDelete","员工批量删除"},logical= Logical.OR)
    public JsonResult batchDelete(Long[] ids){
        try {
            employeeService.batchDeleteByIds(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"批量删除失败!");
        }
        return new JsonResult(true,"批量删除成功");
    }


    // 员工新增或修改
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value={"employee:saveOrUpdate","员工新增或修改"},logical= Logical.OR)
    public JsonResult saveOrUpdate(Employee employee, Long[] roleIds){
        try {
            if(employee.getId() != null){
                employeeService.update(employee, roleIds);
            }else {
                String password = employee.getPassword();
                String name = employee.getName();
                employee.setPassword(new Md5Hash(password, name).toString());
                employeeService.save(employee, roleIds);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"保存或者编辑失败");
        }
        return new JsonResult(true,"保存或者编辑成功");
    }


    // 去新增页面
    @RequestMapping("/input")
    public String input(Long id, Model model){
        List<Department> departments = departmentService.list();
        model.addAttribute("depts", departments);
        List<Role> roles = roleService.list();
        model.addAttribute("roles", roles);
        if(id != null){
            Employee employee = employeeService.get(id);
            model.addAttribute("employee", employee);
        }
        return "employee/input";
    }

    // 删除员工数据
    @RequestMapping("/delete")
    @RequiresPermissions(value={"employee:delete","员工删除"},logical= Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        try {
            employeeService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"删除失败!");
        }
        return new JsonResult(true,"删除成功");
    }

    // 查询所有员工数据
    @RequestMapping("/list")
    @RequiresPermissions(value={"employee:list","员工列表"},logical= Logical.OR)
    public String list(Model model, @ModelAttribute("qo")EmployeeQueryObject qo){
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageResult", pageInfo);

        List<Department> departments = departmentService.list();
        model.addAttribute("depts", departments);

        return "employee/list";
    }
}
